/**
 * @file Xados.cpp
 */

#include <Xados.hpp>

#include <iostream>
using namespace std;

#include <images/images.h>

Xados::Xados(ConfigManager* config) :
	QDialog() {
	/*
	 * Window basic configuration
	 * @{
	 */
	this->_config = config;
	for(int i = 0; i < XADOS_CONF_MBUTTON_COUNT; i++) {
		this->_macroButtons[i] = NULL;
		this->_macroButtonsListeners[i] = NULL;
	}

	for(int i = 0; i < XADOS_CONF_CBOXES_COUNT; i++) {
		this->_checkBoxes[i] = NULL;
		this->_checkBoxesLabels[i] = NULL;
	}

	this->_browser = NULL;

	this->_bakSize = QSize(500, 400);
	this->_largeSize = true;

	this->setWindowIcon(QPixmap(xpmXados_icon64));
	{
		QString title = XADOS_PROGRAM"-"XADOS_VERSION": ";
		title += this->_config->config().c_str();
		this->setWindowTitle(title);
	}
	this->setFixedSize(this->_bakSize);
	/* @} */

	QObject::connect(this, SIGNAL(logoClicked()), this, SLOT(toggleSize()));

	/*
	 * Help Button
	 */
	this->_help = new HelpDialog("/home/raco/workspace/Xados2/help/help/");///@todo
	this->_helpButton = new QPushButton(this);
	this->_helpButton->setGeometry(420, 5, 25, 25);
	this->_helpButton->setIcon(QPixmap(xpmHelpPic));
	this->_helpButton->setFlat(true);
	this->_helpButton->setToolTip("Opens "XADOS_PROGRAM" help");
	this->_helpButton->setShortcut(QKeySequence("F1"));
	QObject::connect(this->_helpButton, SIGNAL(pressed()), this->_help, SLOT(show()));
	QObject::connect(this, SIGNAL(closed()), this->_help, SLOT(close()));
	/* @} */

	/*
	 * Console Button
	 */
	this->_consoleButton = new QPushButton(this);
	this->_consoleButton->setGeometry(445, 5, 25, 25);
	this->_consoleButton->setIcon(QPixmap(xpmCommandLinePic));
	this->_consoleButton->setFlat(true);
	//QObject::connect(this->_consoleButton, SIGNAL(pressed()), this, SLOT(???));
	/* @} */

	/*
	 * Edit Button
	 */
	this->_editButton = new QPushButton(this);
	this->_editButton->setGeometry(470, 5, 25, 25);
	this->_editButton->setIcon(QPixmap(xpmEditorPic));
	this->_editButton->setFlat(true);
	//QObject::connect(this->_editButton, SIGNAL(pressed()), this, SLOT(???));
	/* @} */

	this->loadCheckBoxes();
	this->loadBrowser();
	this->loadMacroButtons();

	/*
	 * Close Button
	 */
	this->_closeButton = new QPushButton(this);
	this->_closeButton->setGeometry(395, 370, 100, 25);
	this->_closeButton->setText("&Close");
	QObject::connect(this->_closeButton, SIGNAL(pressed()), this,
			SLOT(close()));
	/* @} */


	/*
	 * Logo
	 * @{
	 */
	this->_logo = new QLabel(this);
	this->_logo->setScaledContents(true);
	if(this->_config->logo() != "") {
		this->_logo->setPixmap(QPixmap(this->_config->logo().c_str()));
	} else {
		this->_logo->setPixmap(xpmXados_logo);
	}
	this->_logo->setGeometry(5, 10, 100, 380);

	if(this->_config->startSmall()) {
		this->toggleSize();
	}
	/* @} */
}
Xados::~Xados() {
	for(int i = 0; i < XADOS_CONF_MBUTTON_COUNT; i++) {
		if(this->_macroButtons[i]) {
			delete this->_macroButtons[i];
			delete this->_macroButtonsListeners[i];
		}
	}
	for(int i = 0; i < XADOS_CONF_CBOXES_COUNT; i++) {
		if(this->_checkBoxes[i]) {
			delete this->_checkBoxes[i];
			delete this->_checkBoxesLabels[i];
		}
	}
	delete this->_closeButton;
	delete this->_helpButton;
	delete this->_consoleButton;
	delete this->_editButton;
	delete this->_logo;
}

void Xados::catchCheckBoxChange(int) {
	string aux;

	for(int i = 0; i < XADOS_CONF_CBOXES_COUNT; i++) {
		if(this->_checkBoxes[i] && this->_checkBoxes[i]->isChecked()) {
			aux += " " + this->_config->checkBoxes()[i].flag;
		}
	}

	this->_config->setKeyword(this->_config->checkBoxesKeyword(), aux);
}

void Xados::closeEvent(QCloseEvent* event) {
	emit closed();
	this->QDialog::closeEvent(event);
}

void Xados::loadBrowser() {
	if(this->_config->browser().active) {
		this->_browser = new BrowserWidget(this);
		this->_browser->setDirectory(this->_config->browser().directory);
		this->_browser->setGeometry(QRect(110, 50, 280, 315));

		if(this->_config->hasCheckBoxes()) {
			this->_browser->setGeometry(QRect(0, 400, 500, 200));

			this->_bakSize = QSize(500, 600);
			this->setFixedSize(this->_bakSize);
		}
	}
}

void Xados::loadCheckBoxes() {
	if(this->_config->hasCheckBoxes()) {
		for(int i = 0; i < XADOS_CONF_CBOXES_COUNT; i++) {
			if(this->_config->checkBoxes()[i].active) {
				this->_checkBoxes[i] = new QCheckBox(this->_config->checkBoxes()[i].name.c_str(), this);
				this->_checkBoxes[i]->setGeometry(110, 50 + i * 25, 25, 25);

				this->_checkBoxesLabels[i] = new ExtendedQLabel(this->_config->checkBoxes()[i].name.c_str(), this);
				this->_checkBoxesLabels[i]->setGeometry(135, 50 + i * 25, 255, 25);

				if(this->_config->checkBoxes()[i].description != "") {
					this->_checkBoxes[i]->setToolTip(this->_config->checkBoxes()[i].description.c_str());
					this->_checkBoxesLabels[i]->setToolTip(this->_config->checkBoxes()[i].description.c_str());
				}

				QObject::connect(this->_checkBoxesLabels[i], SIGNAL(clicked()), this->_checkBoxes[i], SLOT(toggle()));
				QObject::connect(this->_checkBoxes[i], SIGNAL(stateChanged(int)), this, SLOT(catchCheckBoxChange(int)));
			}
		}
	}
}

void Xados::loadMacroButtons() {
	for(int i = 0; i < XADOS_CONF_MBUTTON_COUNT; i++) {
		if(this->_config->macroButtons()[i].active) {
			this->_macroButtons[i] = new QPushButton(this->_config->macroButtons()[i].name.c_str(), this);
			this->_macroButtons[i]->setGeometry(395, 50 + i * 25, 100, 25);
			if(this->_config->macroButtons()[i].icon != "") {
				this->_macroButtons[i]->setIcon(QPixmap(this->_config->macroButtons()[i].icon.c_str()));
			}
			if(this->_config->macroButtons()[i].description != "") {
				this->_macroButtons[i]->setToolTip(this->_config->macroButtons()[i].description.c_str());
			}
			this->_macroButtonsListeners[i] = new MBListener(i);
			QObject::connect(this->_macroButtonsListeners[i], SIGNAL(pressed(int)), this, SLOT(runMacroButtonAction(int)));
			QObject::connect(this->_macroButtons[i], SIGNAL(pressed()), this->_macroButtonsListeners[i], SLOT(run()));
		}
	}
}

void Xados::mouseDoubleClickEvent(QMouseEvent *e) {
	if(this->_logo->geometry().contains(e->pos())) {
		emit logoClicked();
	} else {
		this->QDialog::mouseDoubleClickEvent(e);
	}
}

int Xados::runCommand(const string &command) {
	if(this->_config->browser().active) {
		vector<string> list = this->_browser->paths();
		vector<string>::iterator i;
		string aux = "";
		for(i = list.begin(); i != list.end(); i++) {
			aux += " \"" + *i + "\"";
		}
		this->_config->setKeyword(this->_config->browser().keyword, aux);
	}

	string auxCommand = this->_config->replaceKeywords(command);
	return system(auxCommand.c_str());
}

void Xados::runMacroButtonAction(int buttonNumber) {
	this->runCommand(this->_config->macroButtons()[buttonNumber].command);
}

void Xados::toggleSize() {
	this->_largeSize = !this->_largeSize;
	if(this->_largeSize) {
		this->setFixedSize(this->_bakSize);
		this->_logo->setGeometry(5, 10, 100, 380);
	} else {
		this->setFixedSize(QSize(100, 380));
		this->_logo->setGeometry(0, 0, 100, 380);
	}
}
